<?php

/**
 * @author Thanh Nguyen Dinh <thanhnguyendinh@admicro.vn>
 * @copyright 2013
 */

class Landing extends MX_Controller
{
    
    public function __construct()
    {
        parent::__construct();
        $this->load->library('Admin_Layout');
		$this->load->model("contact/admin_contact_model");
		$this->load->model("config/admin_config_model");	
		$this->load->library('Minifier');
        $this->load->library('Myvietstring');	
    }

    public function index($page='')
    {    	    	    	
		$this->mycommon->authAdmin();
        $this->admin_layout->_loadHeader("");
        $this->admin_layout->_loadLeftMenu();
		$data = array();		
       	$tmp = $this->admin_contact_model->get_count_contact(1);       	
       	$page = ($page != '') ? intval($page) : 1;
        $this->load->library('pagination');         
        $config['base_url'] = site_url('admin/landing/index');
        $config['total_rows'] = count($tmp);
        $config['per_page'] = 10;
        $config['use_page_numbers'] = TRUE;        
        $config['cur_page'] = $page;
        $config['uri_segment'] = 4;        
        $this->pagination->initialize($config);
        $start = ($page - 1) * $config['per_page'];
        $data['pagination'] = $this->pagination->create_links();
        $data['cur_page'] = $page;
        $data['total_page'] = $this->pagination->num_links;  
        $data['infos'] = $this->admin_contact_model->get_all_contact($start, $config['per_page'],1);        
        if($this->input->post())
		{			
			$option = $this->input->post('applyfield');			
			if(!$option) $option = 0;
			$check_items = $this->input->post('check_item');				
			if($check_items){
				foreach($check_items as $check_item)
				{
					if($option == 1)
						$this->delete($check_item);
					if($option == 2){						
						$contact = array(	'id'=>$check_item,
										 	'status'=>2									
											);
						$result = $this->admin_contact_model->update_contact($contact);
						if($result)
							redirect(site_url('admin/landing/index'));											
					}
				}
			}				
		}	        
        $this->load->view("landing/landing_view",$data);
        $this->admin_layout->_loadFooter();
    }
    
	public function delete($id)
    {
    	$this->mycommon->authAdmin();
    	$result = $this->admin_contact_model->delete_contact($id);    	
    	if($result){    		
    		redirect(site_url('admin/landing'));
    	}	    	
    }
    
    public function update()
    {    	
    	$this->mycommon->authAdmin();
    	$data = array();
    	$data['error'] = "";
    	$contact = $this->input->post();    	
    	if($contact['status'] != 0 && $contact['status'] != 2)
    		$contact['status'] = 1;
    	if($contact['status'] == 0)
    		$result = $this->delete($contact['id']);
    	else    	
    		$result = $this->admin_contact_model->update_contact($contact);
    	if(!$result){
    		 $data['error'] = 1;    		 
    	}    	    		        	
    	echo json_encode($data);
    }
	
    public function confirm($page='')
    {
    	$this->mycommon->authAdmin();    	
    	$this->minifier->AddJsNoPack('highchart/js/highcharts');
    	$this->minifier->AddJsNoPack('highchart/js/modules/exporting');
        $this->admin_layout->_loadHeader("");
        $this->admin_layout->_loadLeftMenu();
		$data = array();			
        $tmp = $this->admin_contact_model->get_count_contact(2);                      
        $data['percent'] = array();
        if($tmp)
        	$data['percent']['total'] = $count_contact = count($tmp);
        else 	
        	$data['percent']['total'] = $count_contact = 0;
        $good = 0; $bad = 0;
        if($count_contact > 0){
	        foreach($tmp as $cont){
	        	if($cont['idea'] == 1)
	        		$good ++;        	
	        	if($cont['idea'] == 3)
	        		$bad ++;        		
	        }
        }
        if($good != 0)
        	$data['percent']['good_idea'] = number_format($good * 100 / $data['percent']['total']);
        else 
        	$data['percent']['good_idea'] = 0;
        if($bad != 0)
        	$data['percent']['bad_idea'] = number_format($bad * 100 / $data['percent']['total']);
        else 
        	$data['percent']['bad_idea'] = 0;
        $data['percent']['not_bad_idea'] = 100 -  $data['percent']['good_idea'] - $data['percent']['bad_idea'];                     
        $page = ($page != '') ? intval($page) : 1;
        $this->load->library('pagination');         
        $config['base_url'] = site_url('admin/landing/phanhoi');
        $config['total_rows'] = $count_contact;
        $config['per_page'] = 10;
        $config['use_page_numbers'] = TRUE;        
        $config['cur_page'] = $page;
        $config['uri_segment'] = 4;        
        $this->pagination->initialize($config);
        $start = ($page - 1) * $config['per_page'];
        $data['pagination'] = $this->pagination->create_links();
        $data['cur_page'] = $page;
        $data['total_page'] = $this->pagination->num_links;  
        $data['infos'] = $this->admin_contact_model->get_all_contact($start, $config['per_page'],2);
        if($this->input->post())
		{
			$option = $this->input->post('applyfield');
			if(!$option) $option = 0;
			$check_items = $this->input->post('check_item');				
			if($check_items){
				foreach($check_items as $check_item)
				{
					if($option == 1)
						$this->delete($check_item);
					if($option == 2){
						$contact = array(	'id'=>$check_item,
										 	'status'=>2									
											);
						$result = $this->admin_contact_model->update_contact($contact);
						if($result)
							redirect(site_url('admin/landing/index'));											
					}
				}
			}		
		}	
        
        $this->load->view("landing/confirm_view",$data);
        $this->admin_layout->_loadFooter(); 	
    }
        
	public function pricefile($id)
    {
    	$this->mycommon->authAdmin();
    	if(!$id || !is_numeric($id)){
    		redirect(site_url('admin'));
    	}
    	$data = array();
        $data['error'] = '';
        $data['success'] = '';
        $this->minifier->AddJsNoPack('jquery.validate');
        $this->minifier->AddJsNoPack('tiny_mce/tiny_mce');
    	$this->admin_layout->_loadHeader(""); 
    	$this->admin_layout->_loadLeftMenu();  	  	  
    	if($this->input->post())
		{		
			$content = $this->input->post('content');											
			$filename = $_FILES['upload']['name'];
           	$url_image = "";
           	if($filename != ""){
            	$type = $_FILES['upload']['type'];
            	if( $type=='application/vnd.openxmlformats-officedocument.wordprocessingml.document' || $type == 'application/x-rar' || $type == 'application/zip' || $type == 'application/pdf' || $type == 'application/msword' || $type == 'application/vnd.ms-powerpoint' || $type=='application/vnd.ms-excel' || $type=='text/plain' || $type=='application/x-rar-compressed' && $_FILES['upload']['size'] < (1024 * 4)){             
	            	$pos = strrpos($filename, '.');
				    $name = substr($filename, 0, $pos);
				    $name = strtolower(str_replace(' ', '-', $this->myvietstring->removeVietChars($name)));
				    $name = $this->myvietstring->removeInvalidCharacter($name);
				    $name = strlen(strlen($name) > 10) ? substr($name, 0, 10) : $name;
				    $name.= '-' . time();				    
				    $fileParts = pathinfo($_FILES['upload']['name']);
			        $targetFile = $name .".".$fileParts['extension'];			        			            
            		$check = move_uploaded_file($_FILES['upload']['tmp_name'],"uploads/document/" . $targetFile);
            		$url_image = "uploads/document/".$name.'.'.$fileParts['extension'];            			            		            		
            		if(!$check)
            			$data['error'] = "Upload không thành công!";
		        }
		        else{
		        	$data['error'] = "Nhập định dạng file của bạn không được hỗ trợ !";
		        }            			            	            				
			}
			if($data['error'] == ""){
				if($url_image != "")         	
		            $info = array(  
		            				'id'=>$id,	            				
									'content'=>$content,
									'file'=>$url_image	            					            				
								);
				else
					$info = array(  
								'id'=>$id,	            				
								'content'=>$content									            					            				
							);
					
					$result = $this->admin_config_model->update_config($info);
					if(!$result)
						$data['error'] = "Cập nhật không thành công!";
					else{ 
						$data['success'] = 'Cập nhật thành công!';					
					}			 		
				} 
			}  
		$data['config'] = $this->admin_config_model->get_config_by_id($id); 	
    	$data['frmToken'] = uniqid();
        $this->session->set_userdata('frmToken',$data['frmToken']);
		$this->load->view("landing/pricefile_view",$data);	
		$this->admin_layout->_loadFooter();    	
    } 
        
}

?>
